import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import { HttpParams } from '@angular/common/http';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-operateyear',
    templateUrl: './operate.component.html'
})
export class OperateYearComponent implements OnInit {
    Visible = false;
    count = 0;
    ids = '';
    ideaValue = '';
    @Input() operateType: any;
    @Input() operateTitle: any;
    @Input() operateData: any;
    @Input() operateVccj: any;

    @Output() public operateCloseModal = new EventEmitter();
    @Output() public operateReloadData = new EventEmitter();

    constructor(
        private message: NzMessageService,
        private http: _HttpClient
    ) { }

    ngOnInit() {
        let status = false;
        for (const item of this.operateData) {
            if (item.checked) {
                this.ids += ',' + item.N_ID;
                this.count = this.count + 1;
                status = true;
            }
        }
        if (status) {
            setTimeout(() => {
                this.Visible = true;
            }, 200);
        } else {
            setTimeout(() => {
                this.Visible = false;
                this.operateCloseModal.emit();
                this.message.create('error', '没有选中记录,请选择');
            }, 200);
        }
    }

    Ok() {
        switch (this.operateTitle) {
            case '删除':
                this.ids = this.ids.replace(',', '');
                this.http.get(environment.apiBase + '/csmis/ymyear/ymtyearwork/getDelYearplanm.jwt?ids=' + this.ids).subscribe((res: any) => {
                    if (res.type === 'success') {
                        this.message.create('success', '删除成功');
                        this.operateReloadData.emit();
                    } else {
                        this.message.create('error', '删除失败');
                    }
                    this.Visible = false;
                    setTimeout(() => {
                        this.operateCloseModal.emit();
                    }, 200);
                });
                break;
            case '提报':
                this.ids = this.ids.replace(',', '');
                const params = new HttpParams()
                    .set('ids', this.ids)
                    .set('vcStatus', this.operateType);
                this.http.post(environment.apiBase + '/csmis/ymyear/ymtyearwork/getyearshenpi.jwt', params).subscribe(res => {
                    if (res.type === 'success') {
                        this.message.create('success', '提报成功');
                        this.operateReloadData.emit();
                    } else {
                        this.message.create('error', '提报失败');
                    }
                    this.Visible = false;
                    setTimeout(() => {
                        this.operateCloseModal.emit();
                    }, 200);
                });
                break;
            case '批准':
            case '驳回':
                if (this.operateTitle === '驳回' && this.ideaValue === '') {
                    this.message.create('error', '驳回必须填写审批意见');
                    return;
                }
                this.ids = this.ids.replace(',', '');
                this.http.get(environment.apiBase + '/csmis/ymyear/ymtyearwork/getyearshenpi.jwt?ids=' + this.ids + '&vcStatus=' + this.operateType + '&appComments=' + this.ideaValue).subscribe((res: any) => {
                    if (res.type === 'success') {
                        this.message.create('success', this.operateTitle + '成功');
                        this.operateReloadData.emit();
                    } else {
                        this.message.create('error', this.operateTitle + '失败');
                    }
                    this.Visible = false;
                    setTimeout(() => {
                        this.operateCloseModal.emit();
                    }, 200);
                });
                break;
            case '强制驳回':
                if (this.ideaValue === '') {
                    this.message.create('error', '驳回必须填写审批意见');
                    return;
                }
                this.ids = this.ids.replace(',', '');
                this.http.get(environment.apiBase + '/csmis/ymyear/ymtyearwork/getyearshenpi.jwt?ids=' + this.ids + '&vcStatus=' + this.operateType + '&appComments=' + this.ideaValue + '&vcCj=' + this.operateVccj).subscribe((res: any) => {
                    if (res.type === 'success') {
                        this.message.create('success', '驳回成功');
                        this.operateReloadData.emit();
                    } else {
                        this.message.create('error', '驳回失败');
                    }
                    this.Visible = false;
                    setTimeout(() => {
                        this.operateCloseModal.emit();
                    }, 200);
                });
                break;
        }
    }

    Cancel() {
        this.Visible = false;
        setTimeout(() => {
            this.operateCloseModal.emit();
        }, 200);
    }
}